package hms.ReservationsModule;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Iterator;

import hms.database.DBRowObject;

public class Guest extends DBRowObject {
	private int roomID;
	private String firstName;
	private String lastName;
	private String address;
	private String city;
	private String state;
	private String country;
	private String gender;
	private String homephone;
	private String mobilephone;
	private String workphone;
	private String comments;
	private Calendar date_of_birth;
	private boolean vip_status;
	private double balance;
	
	
	public Guest() {
		super();
		firstName = "";
		lastName = "";
		gender = "M";
		address = "";
		city = "";
		state = "AL";
		country = "USA";
		homephone = "";
		mobilephone = "";
		workphone = "";
		comments = "";
		date_of_birth = new GregorianCalendar();
	}
	public Guest(int payingCustomerID) {
		super(payingCustomerID);
	}
	/**
	 * @return the roomID
	 */
	public int getRoomID() {
		return roomID;
	}
	/**
	 * @param roomID the roomID to set
	 */
	public void setRoomID(int roomID) {
		this.roomID = roomID;
		markAsChanged();
	}
	/**
	 * @return the firstName
	 */
	public String getFirstName() {
		return firstName;
	}
	/**
	 * @param firstName the firstName to set
	 */
	public void setFirstName(String firstName) {
		this.firstName = firstName;
		markAsChanged();
	}
	/**
	 * @return the lastName
	 */
	public String getLastName() {
		return lastName;
	}
	/**
	 * @param lastName the lastName to set
	 */
	public void setLastName(String lastName) {
		this.lastName = lastName;
		markAsChanged();
	}
	/**
	 * @return the address
	 */
	public String getAddress() {
		return address;
	}
	/**
	 * @param address the address to set
	 */
	public void setAddress(String address) {
		this.address = address;
		markAsChanged();
	}
	/**
	 * @return the city
	 */
	public String getCity() {
		return city;
	}
	/**
	 * @param city the city to set
	 */
	public void setCity(String city) {
		this.city = city;
		markAsChanged();
	}
	/**
	 * @return the state
	 */
	public String getState() {
		return state;
	}
	/**
	 * @param state the state to set
	 */
	public void setState(String state) {
		this.state = state;
		markAsChanged();
	}
	/**
	 * @return the country
	 */
	public String getCountry() {
		return country;
	}
	/**
	 * @param country the country to set
	 */
	public void setCountry(String country) {
		this.country = country;
		markAsChanged();
	}
	/**
	 * @return the gender
	 */
	public String getGender() {
		return gender;
	}
	/**
	 * @param gender the gender to set
	 */
	public void setGender(String gender) {
		this.gender = gender;
		markAsChanged();
	}
	/**
	 * @return the homephone
	 */
	public String getHomephone() {
		return homephone;
	}
	/**
	 * @param homephone the homephone to set
	 */
	public void setHomephone(String homephone) {
		this.homephone = homephone.replaceAll("[^0-9]", "");
		markAsChanged();
	}
	/**
	 * @return the mobilephone
	 */
	public String getMobilephone() {
		return mobilephone;
	}
	/**
	 * @param mobilephone the mobilephone to set
	 */
	public void setMobilephone(String mobilephone) {
		this.mobilephone = mobilephone;
		markAsChanged();
	}
	/**
	 * @return the workphone
	 */
	public String getWorkphone() {
		return workphone;
	}
	/**
	 * @param workphone the workphone to set
	 */
	public void setWorkphone(String workphone) {
		this.workphone = workphone;
		markAsChanged();
	}
	/**
	 * @return the comments
	 */
	public String getComments() {
		return comments;
	}
	/**
	 * @param comments the comments to set
	 */
	public void setComments(String comments) {
		this.comments = comments;
		markAsChanged();
	}
	/**
	 * @return the date_of_birth
	 */
	public Calendar getDate_of_birth() {
		return date_of_birth;
	}
	/**
	 * @param date_of_birth the date_of_birth to set
	 */
	public void setDate_of_birth(Calendar date_of_birth) {
		this.date_of_birth = date_of_birth;
		markAsChanged();
	}
	/**
	 * @return the vip_status
	 */
	public boolean isVip_status() {
		return vip_status;
	}
	/**
	 * @param vip_status the vip_status to set
	 */
	public void setVip_status(boolean vip_status) {
		this.vip_status = vip_status;
		markAsChanged();
	}
	/**
	 * @return the balance
	 */
	public double getBalance() {
		return balance;
	}
	/**
	 * @param balance the balance to set
	 */
	public void setBalance(double balance) {
		this.balance = balance;
		markAsChanged();
	}
	
	public String toString() {
		if((getFirstName() + getLastName()).isEmpty()){
			return "<no name>";
		} else {
			return getFirstName() + " " + getLastName();
		}
	}
	
	
	@Override
	protected String[] getFieldNames() {
		String[] out = {
				"ROOMID",
				"FIRST_NAME",
				"LAST_NAME",
				"ADDRESS",
				"CITY",
				"STATE",
				"COUNTRY",
				"GENDER",
				"HOME_PHONE",
				"MOBILE_PHONE",
				"WORK_PHONE",
				"COMMENTS",
				"DATE_OF_BIRTH",
				"VIP_STATUS"
		};
		return out;
	}
	@Override
	protected void loadPreparedStatement(PreparedStatement stmt)
			throws SQLException {
		stmt.setInt(1, roomID);
		stmt.setString(2, firstName);
		stmt.setString(3, lastName);
		stmt.setString(4, address);
		stmt.setString(5, city);
		stmt.setString(6, state);
		stmt.setString(7, country);
		stmt.setString(8, gender);
		stmt.setString(9, homephone);
		stmt.setString(10, mobilephone);
		stmt.setString(11, workphone);
		stmt.setString(12, comments);
		stmt.setDate(13, new java.sql.Date(date_of_birth.getTimeInMillis()));
		stmt.setBoolean(14, vip_status);
	}
	
	@Override
	protected String getUniqueIdentifierFieldName() {
		return "GUEST_ID";
	}
	@Override
	protected String getTableName() {
		return "GUEST";
	}
	@Override
	protected void loadFromDatabase(ResultSet rs) throws SQLException {
		Iterator<String> i = Arrays.asList(getFieldNames()).iterator();
		roomID = rs.getInt(i.next());
		firstName = rs.getString(i.next());
		lastName = rs.getString(i.next());
		address = rs.getString(i.next());
		city = rs.getString(i.next());
		state = rs.getString(i.next());
		country = rs.getString(i.next());
		gender = rs.getString(i.next());
		homephone = rs.getString(i.next());
		mobilephone = rs.getString(i.next());
		workphone = rs.getString(i.next());
		comments = rs.getString(i.next());
		date_of_birth = GregorianCalendar.getInstance();
		java.sql.Date x = rs.getDate(i.next());
		date_of_birth.setTimeInMillis(x.getTime());
		vip_status = rs.getBoolean(i.next());
	}
	@Override
	protected boolean allowDuplicateRows() {
		return false;
	}
}
